package LinkedListType.JosephRing;

import java.util.Scanner;

public class CircularLinkedListSolution {

    static class Node {
        int data;//该节点的编号
        Node next;//该节点的下一个节点

        public Node(int data, Node next) {
            this.next = next;
            this.data = data;
        }

        public Node() {

        }
    }

    public static void main(String[] args) {
        String[] input = new Scanner(System.in).nextLine().split(" ");
        //表示当前总共n个人，数到数字m时出局
        int n = Integer.parseInt(input[0]);
        int m = Integer.parseInt(input[1]);
        /*构建循环链表*/
        Node head = new Node();
        head.next = null;
        head.data = 0;
        Node p = head;//辅助节点
        for (int i = 1; i <n; i++) {
            Node tmp = new Node(i, null);
            p.next = tmp;
            p = tmp;
        }
        p.next =head;
        p = head;
        /*剔除出局节点*/
        Node tmp = null;
        while (p.next!=p){
            //进行报数
            for (int i = 1; i < m; i++) {
                tmp = p;
                p = p.next;
            }
            //通过循环，此时p为待出局的节点,删除p
            System.out.print(p.data+"\t");
            assert tmp != null;
            tmp.next = p.next;
            p = p.next; //继续进行报数
        }
        System.out.print(p.data+"\t");
    }
}
